home *** CD-ROM | disk | FTP | other *** search
/ IRIX 6.5 Complementary Applications 2004 May / SGI IRIX 6.5 Complementary Applications 2004 May.iso / dist / OpenOffice.idb / usr / OpenOffice / help / en / sbasic.jar / text / sbasic / common / 03090202.xml < prev    next >
Encoding:
Extensible Markup Language  |  2002-01-24  |  5.8 KB  |  76 lines

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <html><head><title>For...Next Statement [Runtime]</title><meta name="filename" content="text/sbasic/common/03090202"/><meta name="language" content="en-US"/><help:css-file-link xmlns:help="http://openoffice.org/2000/help"/><!--The CSS style header method for setting styles--><style type="text/css">
  3.  
  4.         p.P1{
  5.                 }
  6.         p.P2{
  7.                 }
  8.         span.T1{
  9.                 font-weight:bold;}
  10.         </style></head><body>
  11.   
  12.   
  13.   <help:to-be-embedded Eid="fornext" xmlns:help="http://openoffice.org/2000/help">
  14.   <p class="Head1"><help:link Id="66556">For...Next Statement [Runtime]</help:link></p>
  15.   <p class="Paragraph">Repeats the statements between the For....Next block a specified number of times.</p>
  16.   </help:to-be-embedded>
  17.   <p class="P2">Syntax:</p>
  18.   <p class="Paragraph"><help:key-word value="For" tag="kw66556_1" xmlns:help="http://openoffice.org/2000/help"/>For counter=start <help:key-word value="To" tag="kw66556_4" xmlns:help="http://openoffice.org/2000/help"/>To end [<help:key-word value="Step" tag="kw66556_3" xmlns:help="http://openoffice.org/2000/help"/>Step step]</p>
  19.   <p class="Paragraph">statement block</p>
  20.   <p class="P2">[Exit For]</p>
  21.   <p class="Paragraph">statement block</p>
  22.   <p class="Paragraph"><help:key-word value="Next" tag="kw66556_2" xmlns:help="http://openoffice.org/2000/help"/>Next [counter]</p>
  23.   <p class="P2">Variables : Task</p>
  24.   <p class="Paragraph">Counter: Loop counter initially assigned the value to the right of the equal sign (start). Only numeric variables are valid. The loop counter increases or decreases according to the variable Step until End is passed.</p>
  25.   <p class="Paragraph">Start: Numeric variable that defines the initial value at which to begin the loop.</p>
  26.   <p class="Paragraph">End: Numeric variable that defines the final value at which to end the loop.</p>
  27.   <p class="Paragraph">Step: Sets the value by which to increase or decrease the loop counter. If Step is not specified, the incremented value is 1. End must be greater than Start in this case. If you want to decrease Counter, End must be less than Start, and <span class="T1">Step</span> must be assigned a negative value.</p>
  28.   <p class="Paragraph">The <span class="T1">For...Next</span> loop is used to repeat all statements between these two key words as often as specified by the parameters.</p>
  29.   <p class="Paragraph">As the counter variable is decreased, <help:productname xmlns:help="http://openoffice.org/2000/help">%PRODUCTNAME</help:productname> Basic proofs whether the end value has been reached. As soon as the counter passes the end value, the loop automatically ends.</p>
  30.   <p class="Paragraph">It is possible to nest <span class="T1">For...Next</span> statements. If you do not specify a variable following the <span class="T1">Next</span> statement, <span class="T1">Next</span> always automatically refers to the most recent <span class="T1">For</span> statement.</p>
  31.   <p class="Paragraph">If you specify an increment of 0, the statements between <span class="T1">For</span> and <span class="T1">Next</span> are repeated continuously.</p>
  32.   <p class="Paragraph">When counting down the counter variable, <help:productname xmlns:help="http://openoffice.org/2000/help">%PRODUCTNAME</help:productname> Basic checks for overflow or underflow. You need not take care for Counter to equal exactly the value for End. The loop ends when Counter exceeds End (positive Step value) or gets less than End (negative Step value).</p>
  33.   <p class="Paragraph">Using the <span class="T1">Exit For</span> statement, the loop can be exited unconditionally. This statement must be within a <span class="T1">For..Next</span> loop. Use the <span class="T1">If...Then</span> statement to test the exit condition as follows:</p>
  34.   <p class="Paragraph">For...</p>
  35.   <p class="Paragraph">statements</p>
  36.   <p class="Paragraph">If condition = True Then Exit For</p>
  37.   <p class="Paragraph">statements</p>
  38.   <p class="Paragraph">Next</p>
  39.   <p class="Paragraph">Note: In nested <span class="T1">For...Next</span> loops, if you exit a loop unconditionally with <span class="T1">Exit For</span>, only one loop is exited.</p>
  40.   <p class="P2">Example</p>
  41.   <p class="Paragraph">The following example uses two nested loops to sort a string array with 10 elements ( sEntry() ), that are first filled with various contents:</p>
  42.   <p class="Paragraph">Sub ExampleSort</p>
  43.   <p class="Paragraph">Dim sEntry(10) As String</p>
  44.   <p class="Paragraph">Dim iCount As Integer</p>
  45.   <p class="PropText">Dim iCount2 As Integer</p>
  46.   <p class="PropText">Dim sTemp As String</p>
  47.   <p class="PropText">sEntry(1) = "Patty"</p>
  48.   <p class="PropText">sEntry(2) = "Kurt"</p>
  49.   <p class="PropText">sEntry(3) = "Thomas"</p>
  50.   <p class="PropText">sEntry(4) = "Michael"</p>
  51.   <p class="PropText">sEntry(5) = "David"</p>
  52.   <p class="PropText">sEntry(6) = "Cathy"</p>
  53.   <p class="PropText">sEntry(7) = "Susie"</p>
  54.   <p class="PropText">sEntry(8) = "Edward"</p>
  55.   <p class="PropText">sEntry(9) = "Christine"</p>
  56.   <p class="PropText">sEntry(10) = "Jerry"</p>
  57.   <p class="PropText"/>
  58.   <p class="PropText">For iCount = 1 To 10</p>
  59.   <p class="PropText">For iCount2 = iCount + 1 To 10</p>
  60.   <p class="PropText">If sEntry(iCount) > sEntry(iCount2) Then</p>
  61.   <p class="PropText">sTemp = sEntry(iCount)</p>
  62.   <p class="PropText">sEntry(iCount) = sEntry(iCount2)</p>
  63.   <p class="PropText">sEntry(iCount2) = sTemp</p>
  64.   <p class="PropText">End If</p>
  65.   <p class="PropText">Next iCount2</p>
  66.   <p class="PropText">Next iCount</p>
  67.   <p class="PropText"/>
  68.   <p class="PropText">For iCount = 1 To 10</p>
  69.   <p class="PropText">Print sEntry(iCount)</p>
  70.   <p class="PropText">End Sub</p>
  71.   <p class="PropText">For iCount = 1 To 10</p>
  72.   <p class="PropText">Print sEntry(iCount)</p>
  73.   <p class="PropText">Next iCount</p>
  74.   <p class="PropText">End Sub</p>
  75.   <p class="PropText"/>
  76.  </body></html>